1 /** 2 Copyright: Copyright (c) 2018, Joakim Brännström. All rights reserved. 3 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) 4 Author: Joakim Brännström (joakim.brannstrom@gmx.com) 5 */ 6 module code_checker.process; 7 8 import std.exception : collectException; 9 10 import logger = std.experimental.logger; 11 12 struct RunResult { 13 int status; 14 string[] stdout; 15 string[] stderr; 16 17 void print() @safe nothrow const scope { 18 import std.ascii : newline; 19 import std.stdio : writeln; 20 21 foreach (l; stdout) { 22 writeln(l).collectException; 23 } 24 foreach (l; stderr) { 25 writeln(l).collectException; 26 } 27 } 28 } 29 30 RunResult run(string[] cmd) @trusted { 31 import std.array : appender; 32 import std.algorithm : joiner, copy; 33 import std.ascii : newline; 34 import std.process : pipeProcess, tryWait, Redirect; 35 import std.stdio : writeln; 36 import core.thread : Thread; 37 import core.time : dur; 38 39 logger.trace("run: ", cmd.joiner(" ")); 40 41 auto app_out = appender!(string[])(); 42 auto app_err = appender!(string[])(); 43 44 auto p = pipeProcess(cmd, Redirect.all); 45 int exit_status = -1; 46 47 while (true) { 48 auto pres = p.pid.tryWait; 49 50 p.stdout.byLineCopy.copy(app_out); 51 p.stderr.byLineCopy.copy(app_err); 52 53 if (pres.terminated) { 54 exit_status = pres.status; 55 break; 56 } 57 58 Thread.sleep(25.dur!"msecs"); 59 } 60 61 return RunResult(exit_status, app_out.data, app_err.data); 62 }